package com.tarry.aspect;

import com.tarry.aop.Action;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
@Aspect
public class LogAspect {

    public LogAspect(){
        System.out.println("LogAspect create");
    }

    @Pointcut( "@annotation(com.tarry.aop.Action)")
    //@Pointcut( " execution(* com.tarry.service.*(..))")
    public void annotationPoint(){

    }

    @After("annotationPoint()")
    public void after(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);
        System.out.println("注解拦截：" + action.name());
    }

    @Before( "execution(* com.tarry.service.MethodService.*(..))")
    //@Before( " annotationPoint()")
    public void before(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法规则匹配拦截：" + method.getName());
    }
}
